package com.project.JunM.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.JunM.entity.SGather;
import com.project.JunM.entity.SGatherDetails;
import com.project.JunM.mapper.SGatherDetailsMapper;
import com.project.JunM.mapper.SGatherMapper;
import com.project.JunM.service.SGatherService;
import com.project.common.entity.PageVo;
import com.project.common.utils.Serial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SGatherServiceImpl implements SGatherService {

    @Autowired
    private SGatherMapper sGatherMapper;

    @Autowired
    private SGatherDetailsMapper sGatherDetailsMapper;

    @Override
    public PageVo<SGather> queryRegisterCheckAll(Map<String, String> map) {

        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGatherList = sGatherMapper.queryRegisterCheckAll();
        PageInfo pageInfo = new PageInfo(sGatherList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SGather> queryRegisterSelectAll(Map<String, String> map) {

        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGatherList = sGatherMapper.queryRegisterSelectAll();
        PageInfo pageInfo = new PageInfo(sGatherList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SGather> queryPutSetAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGatherList = sGatherMapper.queryPutSetAll();
        PageInfo pageInfo = new PageInfo(sGatherList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SGather> queryPutCheckInAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGatherList = sGatherMapper.queryPutCheckInAll();
        PageInfo pageInfo = new PageInfo(sGatherList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SGather> queryPutCheckInSelectAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGatherList = sGatherMapper.queryPutCheckInSelectAll();
        PageInfo pageInfo = new PageInfo(sGatherList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SGather> queryPutCheckInAuditAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SGather> sGatherList = sGatherMapper.queryPutCheckInAuditAll();
        PageInfo pageInfo = new PageInfo(sGatherList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public int insertSelective(SGather record, String sGatherDetailsJsonObj) throws IOException {
        //入库编号
        Serial serial = new Serial();
        serial.addUser("gatherId","401");
        String gatherId = serial.getNumber("gatherId", "401", null, null, null);
        //把申请单编号 存入实体类中
        record.setGatherId(gatherId);
        //入库申请单 默认值给 S001-0 为等待审核
        record.setCheckTag("S001-0");
        //入库申请单 库存标志
        //K002-0: 未登记未调度
        record.setStoreTag("K002-0");
        sGatherMapper.insertSelective(record);
        //获取入库申请单（SGather）中的主键
        Integer id = record.getId();
        //将json数据转成Java对象（SGatherDetails）
        SGatherDetails[] arr = JSONObject.parseObject(sGatherDetailsJsonObj, SGatherDetails[].class);
        //循环存入数据库中
        for (int i=0;i<arr.length;i++){
            SGatherDetails sGatherDetails = arr[i];
            sGatherDetails.setParentId(id);
            sGatherDetailsMapper.insertSelective(sGatherDetails);
        }
        return 1;
    }

    @Override
    public int insertSelective(SGather record) {
        return sGatherMapper.insertSelective(record);
    }

    @Override
    public int updateSGatherByGatherId(SGather record) {

        return sGatherMapper.updateSGatherByGatherId(record);

    }

    @Override
    public int updateByPrimaryKeySelective(SGather record) {
        return sGatherMapper.updateByPrimaryKeySelective(record);
    }
}
